<?php

function get_option_data($symbol){
    //echo $symbol;
    $mongodb = DB::connection('mongodb');
    $data = $mongodb->collection('optiondatas')->where('symbol',$symbol)->orderBy('id','desc')->first(); 
    //dd($data);
    if(!$data){
        $data['last_price']=0;
        $data['open_price']=0;
        $data['sell_price']=0;
        $data['buy_price']=0;
    }
    return $data;
    //return json_encode($data);     
}

function get_settle_option_data($symbol,$datetime){
    //$response = \Guzzle::get('http://db2015.wstock.cn/wsDB_API2/TheTimeTrend.php?r_type=2&symbol='.$symbol.'&u=fivip100&p=yixi888');
    $response = \Guzzle::get('http://db2015.wstock.cn/wsDB_API/details.php?r_type=2&symbol='.$symbol.'&u=fivip100&p=yixi888&etime='.$datetime);
    
    $response=json_decode($response->getBody()->getContents(),true);
    //dd($response);
    if(empty($response) || isset($response['errcode'])){
        return get_option_data($symbol);
    }

    if(isset($response['NewPrice'])){
        $response[0]['last_price'] = $response['NewPrice'];
    }elseif(isset($response[0])){
        $response[0]['last_price'] = $response[0]['NewPrice'];
    }else{
        return get_option_data($symbol);
    }

    if($symbol=='PMAU'){//伦敦金
        $response[0]['last_price'] = sprintf("%.2f",$response[0]['last_price']);
    }else if($symbol=='PMUDI'){//美元
        $response[0]['last_price'] = sprintf("%.3f",$response[0]['last_price']);
    }else if($symbol=='CEDAXA0'){//德指
        $response[0]['last_price'] = sprintf("%.1f",$response[0]['last_price']);
    }else if($symbol=='WICMECM0'){//欧元
        $response[0]['last_price'] = sprintf("%.5f",$response[0]['last_price']);
    }

    return $response[0];   
    
}

function get_period_data($symbol){
    $response = \Guzzle::get('http://db2015.wstock.cn/wsDB_API2/TheTimeTrend.php?r_type=2&symbol='.$symbol.'&u=fivip100&p=yixi888');
    //dd($response->getBody());
    //return false;
    $response=json_decode($response->getBody()->getContents());
    //var_dump($response);
    return $response;  
}

function get_k_data($symbol,$time){
    $response = \Guzzle::get('http://db2015.wstock.cn/wsDB_API2/kline.php?r_type=2&symbol='.$symbol.'&u=fivip100&p=yixi888&qt_type='.$time.'&return_t=3');
    //dd($response->getBody());
    //return false;
    $response=json_decode($response->getBody()->getContents());
    //var_dump($response);
    return $response;  
}

function get_bond_data($code){
    $client = \Guzzle::getFacadeRoot();
    $response = $client->request('get','http://api.wmcloud.com/data/v1/api/master/getSecHalt.json?field=&ticker='.$code.'&secID=&beginDate=&endDate=',['verify'=>false,'headers'=>['Authorization'=>'Bearer a2c510828670a343b566eedf769a54c0bd497dfb0bc0c6c37fc2fe9845f6972e']]);
    $response=json_decode($response->getBody()->getContents());
    return $response;
}

function get_trade_data($beginDate,$endDate){
    $client = \Guzzle::getFacadeRoot();
    $response = $client->request('get','http://api.wmcloud.com/data/v1/api/master/getTradeCal.json?field=&exchangeCD=XSHG,XSHE&beginDate='.$beginDate.'&endDate='.$endDate,['verify'=>false,'headers'=>['Authorization'=>'Bearer a2c510828670a343b566eedf769a54c0bd497dfb0bc0c6c37fc2fe9845f6972e']]);
    $response=json_decode($response->getBody()->getContents());
    return $response;
}

function get_new_data($code){
    $client = \Guzzle::getFacadeRoot();
    $response = $client->request('get','http://api.wmcloud.com/data/v1/api/equity/getEquIPO.json?field=&ticker='.$code.'&secID=&eventProcessCD=&beginDate=&endDate=',['verify'=>false,'headers'=>['Authorization'=>'Bearer a2c510828670a343b566eedf769a54c0bd497dfb0bc0c6c37fc2fe9845f6972e']]);
    $response=json_decode($response->getBody()->getContents());
    return $response;
}

function get_detail_data($code){
    $codes = explode(',',$code);
    foreach($codes as $k=>$code){
        $a = substr( $code, 0, 1 );
        if($a=='6'){
            $code = $code.'.XSHG';
        }else{
            $code = $code.'.XSHE';
        }
        $codes[$k] = $code;
    }
    $code = implode(',', $codes);
    //dd($code);
    $client = \Guzzle::getFacadeRoot();
    $response = $client->request('get','http://api.wmcloud.com/data/v1/api/market/getTickRTSnapshot.json?securityID='.$code.'&field=',['verify'=>false,'headers'=>['Authorization'=>'Bearer a2c510828670a343b566eedf769a54c0bd497dfb0bc0c6c37fc2fe9845f6972e']]);
    $response=json_decode($response->getBody()->getContents());
    //dd($response);
    if($response->retCode==1){
        return $response->data;
    }else{
        return null;
    }
    //dd($response);
    //return $response;
}

//腾讯接口获取股票信息
function get_detail_data_two($code)
{
    if(empty($code)){
        return null;
    }
//    $code="000858,601007";
    //1.获取股票代码判断是深还是沪，
    $codes=explode(',',$code);
    foreach($codes as $k=>$code){
        $a = substr( $code, 0, 1 );
        if($a=='6'){
            $code = 'sh'.$code;
        }else{
            $code = 'sz'.$code;
        }
        $codes[$k] = $code;
    }
    $code = implode(',', $codes);
    //2.拼接股票code请求接口，
    $backData=\Guzzle::get('http://qt.gtimg.cn/q='.$code);

    $response=$backData->getBody()->getContents();
    $response = mb_convert_encoding($response, 'utf-8', 'gbk');
    //3.构造数据，
    if($response){
        preg_match_all('/=(.*?);/ism',$response,$preg);
        $response=[];
        $responseArr=[];
        foreach($preg[1] as $value){
            $arr=explode('~',$value);
            $response['ticker']=$arr[2];
            $response['lastPrice']=$arr[3];
            $response['openPrice']=$arr[5];
            $response['shortNM']=$arr[1];
            $responseArr[$arr[2]]=(object)$response;
        }
        return $responseArr;
    }else{
        return null;
    }


}